package com.hxxy.wyp.petsLover.common.rest.base.backend;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hxxy.wyp.petsLover.RestBase;
import com.hxxy.wyp.petsLover.common.entities.User;
import com.hxxy.wyp.petsLover.common.entities.UserShare;
import com.hxxy.wyp.petsLover.common.entities.vo.req.DeleteAndOpenOrCloseStatusCommon;
import com.hxxy.wyp.petsLover.common.entities.vo.req.SearchObj;
import com.hxxy.wyp.petsLover.common.entities.vo.req.SearchObjShare;
import com.hxxy.wyp.petsLover.common.services.base.backend.UserShareService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName UserShareRest
 * @Description TODO
 * @Author wyp
 * @Date 2023/2/1 16:00
 * @Version 1.0
 */
@RestController
@ResponseBody
@RequestMapping("/share")
public class UserShareRest extends RestBase {
    @Autowired
    private UserShareService userShareService;

    @SaCheckLogin
    @SaCheckRole(value = {"user_admin", "super_admin"}, mode = SaMode.OR)
    @SaCheckPermission(value = {"user_load","all"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Object page(@RequestBody SearchObjShare searchObjShare) {
        long page = Long.parseLong(searchObjShare.getPage());
        long pageSize = Long.parseLong(searchObjShare.getPageSize());
        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<UserShare> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Strings.isNotBlank(searchObjShare.getUserId()), UserShare::getUserId, searchObjShare.getUserId());
        lambdaQueryWrapper.like(Strings.isNotBlank(searchObjShare.getShareName()), UserShare::getShareName, searchObjShare.getShareName());
        lambdaQueryWrapper.like(Strings.isNotBlank(searchObjShare.getStatus()), UserShare::getStatus, searchObjShare.getStatus());
        lambdaQueryWrapper.orderByDesc(UserShare::getShareTime);
        userShareService.pageSearch(pageInfo, lambdaQueryWrapper);
        return ajaxSuccess(pageInfo, "200", "查询成功！");
    }

    /**
     * 通用型接口
     *
     * @param common
     * @return
     */
    @SaCheckLogin
    @SaCheckRole(value = {"user_admin", "super_admin"}, mode = SaMode.OR)
    @SaCheckPermission(value = {"user_update","all"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    @RequestMapping(value = "/deleteAndOpenOrCloseStatus")
    public Object deleteOne(@RequestBody DeleteAndOpenOrCloseStatusCommon common) {
        String type = "";
        boolean flag = false;
        if ("delete".equals(common.getType())) {
            flag = userShareService.removeById(common.getId());
            type = "delete";
        } else if ("status".equals(common.getType())) {
            UserShare us = userShareService.getById(common.getId());
            String status = us.getStatus();
            if ("0".equals(status)) {
                us.setStatus("1");
            } else {
                us.setStatus("0");
            }
            flag = userShareService.updateById(us);
            type = "status";
        } else if ("update".equals(common.getType())) {
            flag = userShareService.updateById(common.getUserShare());
            type = "update";
        }
        if ("delete".equals(type) && (flag == true)) {
            return ajaxSuccess("200", "删除成功!");
        } else if ("delete".equals(type) && (flag == false)) {
            return ajaxError("500", "删除失败!");
        } else if ("status".equals(type) && flag == true) {
            return ajaxSuccess("200", "更新审核状态成功!");
        } else if ("status".equals(type) && flag == false) {
            return ajaxSuccess("200", "更新审核状态失败!");
        } else if ("update".equals(type) && flag == true) {
            return ajaxSuccess("200", "编辑用户分享成功!");
        } else if ("update".equals(type) && flag == false) {
            return ajaxSuccess("200", "编辑用户分享失败!");
        }
        return ajaxError("系统错误");
    }

}
